import os
from building import *

cwd = GetCurrentDir()

Import('asenv')
ASROOT = asenv['ASROOT']

objs = []

asenv.Append(CPPDEFINES='configTOTAL_HEAP_SIZE=0x5000000')
asenv.Append(CPPDEFINES='PAGE_SIZE=0x1000')
asenv.Append(CPPDEFINES='OS_TICKS_PER_SECOND=1000')
asenv.Append(CPPDEFINES='USECONDS_PER_TICK=10000')
if(not IsPlatformWindows()):
    asenv.Append(CPPDEFINES='ENABLE_SHELL_ECHO_BACK')
asenv.Append(CPPDEFINES='OS_STK_SIZE_SCALER=4')

lds = '%s/script/linker-app.lds'%(cwd)
asenv.Append(LINKFLAGS='-static -T %s'%(lds))
asenv.Append(LINKFLAGS='-Map build/virt/virt.map')

MODULES = ['ECUM','SCHM','MCU','DET',
           'CLIB_ASHEAP','CLIB_MISCLIB',
           'CLIB_MBOX','CLIB_QSORT',
           'CLIB_STDIO_PRINTF',
           'SHELL','CLIB_STRTOK_R',
           'PTHREAD', 'PTHREAD_SIGNAL',
           'TEST',
           'ASKAR',
           ]

MODULES += ['PCI',
            'FATFS','LWEXT4','VFS','DEV',
            'LWIP','FTP',
            'SOAD','DOIP','SD',
            'EEP','FLS','EA','FEE','MEMIF','NVM',
            'DEM',
            'CLIB_STDIO_CAN',
            'CAN','CANIF','PDUR','COM','COMM','CANTP','CANNM',
            'DCM','CANNM','CANSM','NM','OSEKNM','XCP'
            ]

MODULES += ['FDT']

ARCH='virt'
arch='arm64'

if(asenv['MODULES'] is None):
    asenv['MODULES'] = MODULES
else:
    MODULES = asenv['MODULES']

asenv['ARCH'] = ARCH
asenv['arch'] = arch

if('run' in COMMAND_LINE_TARGETS):
    qemu = Qemu()
    target = '%s/release/%s/virt'%(ASROOT,asenv['RELEASE'])
    if(IsPlatformWindows()): target = target + '.exe'
    qemu.Run('-M virt -cpu cortex-a57 -smp 2 -kernel %s'%(target))

asenv.Append(CPPPATH=['%s/../board.posix/common'%(cwd)])

if('MCU' in MODULES):
    objs += Glob('%s/../board.posix/common/Mcu*.c'%(cwd))

if('EEP' in MODULES):
    objs += Glob('%s/../board.posix/common/Eep*.c'%(cwd))

if('FLS' in MODULES):
    objs += Glob('%s/../board.posix/common/Fls*.c'%(cwd))

asenv.Append(CCFLAGS=['-mcpu=cortex-a57+nofp'])

Return('objs')
